package miaofusdk

import (
	"encoding/json"
	"errors"
	"fmt"
)

/**
批次号
*/
type TradeNoBean struct {
	Id       int    `json:"id,omitempty,string"`
	DeviceId string `json:"device_id,omitempty"`
	BatchNo  int    `json:"batch_no,omitempty,string"`
}

type RespTradeNo struct {
	Response
	Data TradeNoBean `json:"data,omitempty"`
}

func (s *SrvShopManager) GetTradeNo(device_id string) (TradeNoBean, error) {
	resp := RespTradeNo{}
	http_request := s.GetPost(s.GetUrl() + "/tradeno/query")
	http_request.Param("device_id", device_id)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(fmt.Sprintf("%s:%s", resp.Code, resp.Msg))
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) AddTradeNo(device_id string) (TradeNoBean, error) {
	resp := RespTradeNo{}
	http_request := s.GetPost(s.GetUrl() + "/tradeno/add")
	http_request.Param("device_id", device_id)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(fmt.Sprintf("%s:%s", resp.Code, resp.Msg))
	} else {
		return resp.Data, nil
	}
}
